﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

namespace AlfarEngine.Utilities
{
    public class Helpers
    {
        /// <summary>
        /// Allow to check if two float are nearly equal (accounting for floating point imprecision)
        /// </summary>
        /// <param name="a">The first float to test</param>
        /// <param name="b">The second float to test</param>
        /// <param name="epsilon">The epsilon for which the float are considered equal (usually Float.Epsilon)</param>
        /// <returns>true if nearly equal, false otherwise</returns>
        public static bool NearlyEqual(float a, float b, float epsilon)
        {
            
            float absA = Math.Abs(a);
            float absB = Math.Abs(b);
            float diff = Math.Abs(a - b);

            if (a * b == 0) { // a or b or both are zero
                // relative error is not meaningful here
                return diff < (epsilon * epsilon);
            } else { // use relative error
                return diff / (absA + absB) < epsilon;
            }
        }

        /// <summary>
        /// Serialize a Vector2 to String as x;y
        /// </summary>
        /// <param name="pVec"></param>
        /// <returns></returns>
        static public string Vector2ToString(Vector2 pVec)
        {
            return pVec.X + ";" + pVec.Y;
        }
    }
}
